<layout name='Layout/layout_page' />
	
	<ol class="breadcrumb">
		<li><a href="<{:U('Goods/index')}>">商品管理</a></li>
      	<li class="active">商品规格</li>
    </ol>
    
    <form id="ajaxForm" action="<{:U('Goods/savepram')}>" method="post" class="form-horizontal form-validata">

    <div class="page-header">
  		<h3><{$goods['name']}><small><a class="btn btn-default btn-sm" href="javascript:void(0)" id="add-spec" onclick="addSpec()">添加规格</a></small></h3>
	</div>       
		<div id="ruleBox">      
            <?php  if(is_array($allspecs)) { foreach($allspecs as $spec) { ?>
            <div class="spec_item" id="spec_<?php  echo $spec['id'];?>">
                <input name="spec_id[]" type="hidden" class="spec_id" value="<?php  echo $spec['id'];?>"/>
                <div class="form-group">
                    <label class="col-md-1 control-label" for="title">规格名称</label>
                    <div class="col-md-2">
                        <input type="text"
                        class="form-control spec_title"
                         name="spec_title[<?php  echo $spec['id'];?>]"
                        id="spec_title[<{$spec['id']}>]"
                        value="<?php  echo $spec['title'];?>"
                        placeholder="如：颜色、口味等"
                        data-rule-required="true"
                        />
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-1 control-label" for="title">规格项</label>
                    <div class="col-md-10">
                        <div class="row spec_item_items" id="spec_item_<?php  echo $spec['id'];?>">
                            <?php  if(is_array($spec['items'])) { foreach($spec['items'] as $specitem) { ?>
                            <div class="col-md-2 spec_item_item">
                                <div class="input-group">
                                    <input type="hidden" class="spec_item_show" name="spec_item_show_<?php  echo $spec['id'];?>[]" value="<?php  echo $specitem['show'];?>">
                                    <input type="text"
                                    name="spec_item_title_<?php  echo $spec['id'];?>[]"
                                    value="<?php  echo $specitem['title'];?>"
                                    data-rule-required="true"
                                    class="form-control input-sm spec_item_title"
                                    />
                                    <input type="hidden" class="spec_item_id" name="spec_item_id_<?php  echo $spec['id'];?>[]" value="<?php  echo $specitem['id'];?>">
                                    <div class="input-group-addon delHand" onclick="removeSpecItem(this)">删除</div>
                                </div>
                            </div>
                            <?php  } } ?>
                        </div>              
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-11 col-sm-offset-1">
                        <button type="button" class="btn btn-info" id="add-specitem-<?php  echo $spec['id'];?>" specid="<?php  echo $spec['id'];?>" onclick="addSpecItem('<?php  echo $spec['id'];?>')">添加项</button>
                        <button type="button" class="btn btn-info" onclick="removeSpec('<?php  echo $spec['id'];?>')">删除规格</button>
                    </div>
                </div>
            </div>
            <?php  } } ?>      
        </div>

		<p><button type="button" class="btn btn-success" onclick="calc()">刷新规格参数表</button></p>
		
		<div id="options"><{$html}></div>

        <div class="form-group">
            <div class="col-md-12">
                <label class="checkbox-inline">
                    <input type="checkbox" <?php if($goods['hasoption']==1){echo 'checked="checked"';}?> id="hasoption" name="hasoption" value="1"> 启用规格设置
                </label>
            </div>
        </div>
		
		<div class="form-group">
			<div class="col-md-12">
				<button type="submit" class="btn btn-info">保存商品规格</button>
			</div>
		</div>
        <input type="hidden" name="goodsID" value="<{$goodsID}>">
	</form>


<script>
	var rsource_url="<?php echo RESOURCE_ROOT;?>";
	var shop_spec_url="<{:U('Goods/getspec')}>";
	var shop_specitem_url="<{:U('Goods/specitem')}>";
</script>

<script language="javascript">
function addSpec(){
	$("#add-spec").html("正在处理...").attr("disabled","true").toggleClass("btn-primary");
	var t=shop_spec_url;
	$.ajax({
		url:t,
		success:function(t){
			$("#add-spec").html('<i class="icon-plus"></i> 添加规格').removeAttr("disabled").toggleClass("btn-primary");
			$("#ruleBox").append(t);
			var e=$(".add-specitem").length-1;
			$(".add-specitem:eq("+e+")").focus();
			window.optionchanged=!0;
		}
	})
}

function removeSpec(t){
	confirm("确认要删除此规格?")&&($("#spec_"+t).remove(),window.optionchanged=!0)
}

function addSpecItem(t){
	$("#add-specitem-"+t).html("正在处理...").attr("disabled","true");
	var e=shop_specitem_url+"&specid="+t;
	$.ajax({
		url:e,
		success:function(e){
			$("#add-specitem-"+t).html("添加规格项").removeAttr("disabled");
			$("#spec_item_"+t).append(e);
			var i=$("#spec_"+t+" .spec_item_title").length-1;
			$("#spec_"+t+" .spec_item_title:eq("+i+")").focus();
			window.optionchanged=!0
		}
	})
}
function removeSpecItem(t){
	$(t).parent().parent().remove()
}

function calc() {
    window.optionchanged = !1;
    var t = '<table style="width:100%;" class="table table-bordered"><thead><tr>',
    e = [];
	if($(".spec_item").length==0){$("#options").html("");return;}
    $(".spec_item").each(function(t) {
        var i = $(this),
        n = {
            id: i.find(".spec_id").val(),
            title: i.find(".spec_title").val()
        },
        o = [];
        i.find(".spec_item_item").each(function() {
            var t = $(this),
            e = {
                id: t.find(".spec_item_id").val(),
                title: t.find(".spec_item_title").val(),
                show: t.find(".spec_item_show").get(0).checked ? "1": "0"
            };
            o.push(e)
        }),
        n.items = o,
        e.push(n)
    }),
    e.sort(function(t, e) {
        return t.items.length > e.items.length ? 1 : t.items.length < e.items.length ? -1 : void 0
    });
    for (var i = e.length,n = 1,o = new Array(i),p = new Array(i), s = 0; i > s; s++) {
        t += "<th>" + e[s].title + "</th>";
        var a = e[s].items.length;
        0 >= a && (a = 1),
        n *= a,
        o[s] = new Array(n);
        for (var c = 0; n > c; c++) o[s][c] = new Array; {
            e[s].items.length
        }
        for (p[s] = 1, c = s + 1; i > c; c++) p[s] *= e[c].items.length
    }
    t += '<th>库存</th>',
    t += '<th>销售价格</th>',
    t += '<th>市场价格</th>',
    t += '<th>重量(克)</th>',
    t += "</tr>";
    for (var d = 0; i > d; d++) 
	for (var l = 0, r = 0,c = 0; n > c; c++) {
        var _ = p[d];
        c % _ == 0 ? o[d][c] = {
            title: e[d].items[l].title,
            html: "<td rowspan='" + _ + "'>" + e[d].items[l].title + "</td>\r\n",
            id: e[d].items[l].id
        }: o[d][c] = {
            title: e[d].items[l].title,
            html: "",
            id: e[d].items[l].id
        },
        r++,
        r == _ && (l++, l > e[d].items.length - 1 && (l = 0), r = 0)
    }
    for (var h = "", s = 0; n > s; s++) {
        h += "<tr>";
        for (var m = [], u = [], c = 0; i > c; c++) h += o[c][s].html,
        m.push(o[c][s].id),
        u.push(o[c][s].title);
        m = m.join("_"),
        u = u.join("+");
        var v = {
            id: "",
            title: u,
            stock: "",
            costprice: "",
            productprice: "",
            marketprice: "",
            weight: ""
        };
        $(".option_id_" + m).length > 0 && (v = {
            id: $(".option_id_" + m + ":eq(0)").val(),
            title: u,
            stock: $(".option_stock_" + m + ":eq(0)").val(),
            costprice: $(".option_costprice_" + m + ":eq(0)").val(),
            productprice: $(".option_productprice_" + m + ":eq(0)").val(),
            marketprice: $(".option_marketprice_" + m + ":eq(0)").val(),
            weight: $(".option_weight_" + m + ":eq(0)").val()
        }),
        h += "<td>",
        h += '<input name="option_stock_' + m + '[]"  type="text" class="form-control option_stock option_stock_' + m + '" value="' + ("undefined" == v.stock ? "": v.stock) + '"/></td>',
        h += '<input name="option_id_' + m + '[]"  type="hidden" class="form-control option_id option_id_' + m + '" value="' + ("undefined" == v.id ? "": v.id) + '"/>',
        h += '<input name="option_ids[]"  type="hidden" class="form-control option_ids option_ids_' + m + '" value="' + m + '"/>',
        h += '<input name="option_title_' + m + '[]"  type="hidden" class="form-control option_title option_title_' + m + '" value="' + ("undefined" == v.title ? "": v.title) + '"/></td>',
        h += "</td>",        
        h += '<td><input name="option_productprice_' + m + '[]" type="text" class="form-control option_productprice option_productprice_' + m + '" " value="' + ("undefined" == v.productprice ? "": v.productprice) + '" data-rule-required="true"/></td>',
        h += '<td><input name="option_marketprice_' + m + '[]" type="text" class="form-control option_marketprice option_marketprice_' + m + '" value="' + ("undefined" == v.marketprice ? "": v.marketprice) + '"/></td>',
        h += '<td><input name="option_weight_' + m + '[]" type="text" class="form-control option_weight option_weight_' + m + '" " value="' + ("undefined" == v.weight ? "": v.weight) + '"/></td>',
        h += "</tr>"
    }
    t += h,
    t += "</table>",
    $("#options").html(t)
}

function setCol(t) {
    $("." + t).val($("." + t + "_all").val())
}
</script>
<input type="hidden" id="menu_action" value="goods" />